﻿Imports System.Globalization
Imports System.Net
Imports RFDeviceNet
Imports System.Text

Module Module1

    Private Const MainMenu As String = "****************************************************" & vbCr & vbLf & "Press key to select:" & vbCr & vbLf & "1 - Test 620 device." & vbCr & vbLf & "2 - Test 603 device." & vbCr & vbLf & "Other - Exit."

    Private _pworker As PacketWorker

    Sub Main()
        _pworker = New PacketWorker()
        Dim init = RFDeviceObjectNet.Initialize()
        If Not init Then
            Console.WriteLine("RFDeviceObject initializing failed. Program will exit.")
            Console.Read()
            Return
        End If
        Console.WriteLine("Library version - " & RFDeviceObjectNet.RFDevice_GetVersionString())
        While True
            Console.WriteLine(MainMenu)
            Dim key = Console.ReadKey()
            Console.WriteLine()
            If key.Key = ConsoleKey.D1 OrElse key.Key = ConsoleKey.NumPad1 Then
                Go620Test()
            ElseIf key.Key = ConsoleKey.D2 OrElse key.Key = ConsoleKey.NumPad2 Then
                Go603Test()
            Else
                Exit While
            End If
        End While

    End Sub

    Private Sub Go603Test()

        Console.WriteLine("603 test...")
        Dim dev = RF603DeviceNet.CreateRF603Device()
        dev.BindNetworkAddress(1)
        Dim comNum As Integer
        While True
            Console.WriteLine("Input COM port number for 603 device connection. Press 'Enter' to complete.")
            Dim comNumStr = Console.ReadLine()
            If String.IsNullOrEmpty(comNumStr) OrElse Not Integer.TryParse(comNumStr, comNum) Then
                Console.WriteLine("Wrong number.")
            Else
                Exit While
            End If
        End While
        If Not dev.OpenPort("COM" & comNum.ToString(CultureInfo.InvariantCulture), 9600) Then
            Console.WriteLine("OpenPort operation error.")
            Return
        End If
        Dim ps = New _RF60x_HELLO_ANSWER_Net()
        If Not dev.HelloCmd(1, ps) Then
            Console.WriteLine("HelloCmd opearion error.")
        Else
            Console.WriteLine("Device serial: " & Convert.ToString(ps.DeviceSerial))
            Console.WriteLine("Device max distance: " & Convert.ToString(ps.DeviceMaxDistance))
        End If
        Dim value As UShort = 0
        If Not dev.Measure(1, value) Then
            Console.WriteLine("Can't get measure from 603 device.")
        End If
        Console.WriteLine("Read measure value: " & value.ToString(CultureInfo.InvariantCulture))

        If Not dev.StartStream(1) Then
            Console.WriteLine("Can't start stream from 603 device.")
        Else
            For i = 0 To 9
                If dev.GetStreamMeasure(value) Then
                    Console.Write(value & " ")
                Else
                    Console.Write("GetStreamMeasureError.")
                    Exit For
                End If
            Next
            Console.WriteLine()
            Console.WriteLine(If(Not dev.StopStream(1), "StopStream operation error.", "StopStream operation complete."))
            Console.WriteLine(If(Not dev.ClosePort(), "ClosePort operation error.", "ClosePort operation complete."))
        End If


    End Sub

    Private Sub Go620Test()
        Console.WriteLine("620 test...")

        Dim block As _UDP_DEVINFOBLOCK_PC_Net
        Try
            block = _pworker.GetUdpPcketFrom620()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Return
        End Try

        Dim dev = RF620DeviceNet.CreateRF620Device(block)
        Dim sb = New StringBuilder()
        For Each b As Byte In block.IP
            sb.Append(b.ToString(CultureInfo.InvariantCulture) + "."c)
        Next
        sb.Remove(sb.Length - 1, 1)
        If dev.Connect() Then
            Console.WriteLine("Connected to 620 device " & Convert.ToString(sb) & "."c)
            Console.WriteLine(If(dev.ReadParams(), "Parameters reading from 620 device done.", "Can't read parameters from 602 device."))
            Console.WriteLine(If(dev.WriteParams(), "Parameters writing to 620 device done.", "Can't write parameters to 602 device."))
            Dim tcpResultBuffer = New [Byte]() {}
            If dev.GetResult(tcpResultBuffer) Then
                Console.WriteLine("TCP GetResult operation result:")
                Dim i As Integer = 0
                For Each b As Byte In tcpResultBuffer
                    Console.Write("{0,3:D}", b)
                    If i > 9 Then
                        i = 0
                        Console.WriteLine()
                    Else
                        i += 1
                        Console.Write(" ")
                    End If
                Next
                Console.WriteLine()
            Else
                Console.WriteLine("TCP GetResult operation error.")
            End If
            Console.WriteLine(If(dev.Disconnect(), "620 device disconnected.", "620 device disconnect error."))
        Else
            Console.WriteLine("TCP connecting to 620 device failed.")
        End If

        While True
            Console.WriteLine("Input local IP for UDP working with 620 device. Use 'Enter' on complete.")
            Dim ip = Console.ReadLine()
            Console.WriteLine()
            Dim ipAddress1 As IPAddress
            If ip Is Nothing OrElse IPAddress.TryParse(ip, ipAddress1) = False Then
                Console.WriteLine("Wrong ip.")
            Else
                Console.WriteLine("Current ip: " & ipAddress1.ToString())
                If Not dev.UDPConnect(6001, ipAddress1.ToString()) Then
                    Console.WriteLine("Can't connect with 620 device via UDP.")
                    Exit While
                End If
                Dim udpBuf = New Byte() {}
                If Not dev.UDPGetResult(udpBuf) Then
                    Console.WriteLine("Can't get result from 620 device via UDP.")
                    Exit While
                End If
                Dim i As Integer = 0
                For Each b As Byte In udpBuf
                    Console.Write("{0,3:D}", b)
                    If i > 9 Then
                        i = 0
                        Console.WriteLine()
                    Else
                        i += 1
                        Console.Write(" ")
                    End If
                Next
                If Not dev.Disconnect() Then
                    Console.WriteLine("Can't disconnect from 620 device via UDP.")
                    Exit While
                End If
                Console.WriteLine("Disconnected from 620 device via UDP.")
                Exit While
            End If
        End While

    End Sub

End Module
